
package com.usersmodule.dao;
import com.usersmodule.bean.TbUserRoleMiddle;

import java.sql.*;
import java.util.*;
import util.*;
public class TbUserRoleMiddleDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbUserRoleMiddle tbUserRoleMiddle=null;
    private List<TbUserRoleMiddle> tbUserRoleMiddleList=null;

    public boolean add(TbUserRoleMiddle inTbUserRoleMiddle){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into TbUserRoleMiddle(UserId,RoleId) values(?,?)";
        pt = conn.prepareStatement(sql);
        pt.setInt(1,inTbUserRoleMiddle.getUserId());
        pt.setInt(2,inTbUserRoleMiddle.getRoleId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public boolean del(int inUserRoleMiddleId){
        return UtilDAO.del("TbUserRoleMiddle", "UserRoleMiddleId", inUserRoleMiddleId);
    }
    public boolean edit(TbUserRoleMiddle inTbUserRoleMiddle){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="update TbUserRoleMiddle set UserId=?,RoleId=?,CreateTime=getdate() where UserRoleMiddleId=?";
        pt = conn.prepareStatement(sql);
        pt.setInt(1,inTbUserRoleMiddle.getUserId());
        pt.setInt(2,inTbUserRoleMiddle.getRoleId());
        pt.setInt(3,inTbUserRoleMiddle.getUserRoleMiddleId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public TbUserRoleMiddle getTbUserRoleMiddleByUserRoleMiddleId(int inUserRoleMiddleId) {
        tbUserRoleMiddle=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from TbUserRoleMiddle where UserRoleMiddleId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inUserRoleMiddleId);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbUserRoleMiddle = getTbUserRoleMiddle(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbUserRoleMiddle;
    }
    public List<TbUserRoleMiddle> getTbUserRoleMiddleAll() {
        tbUserRoleMiddleList=new ArrayList<TbUserRoleMiddle>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from TbUserRoleMiddle";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbUserRoleMiddleList.add(getTbUserRoleMiddle(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbUserRoleMiddleList;
    }
    public List<TbUserRoleMiddle> getTbUserRoleMiddleAll(VskPage vp) {
        tbUserRoleMiddleList=new ArrayList<TbUserRoleMiddle>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from TbUserRoleMiddle";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "TbUserRoleMiddle", "UserRoleMiddleId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbUserRoleMiddleList.add(getTbUserRoleMiddle(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbUserRoleMiddleList;
    }
    public TbUserRoleMiddle getTbUserRoleMiddle(ResultSet rs) throws Exception{
        TbUserRoleMiddle tbUserRoleMiddle = new TbUserRoleMiddle();
        tbUserRoleMiddle.setUserRoleMiddleId(rs.getInt("UserRoleMiddleId"));
        tbUserRoleMiddle.setUserId(rs.getInt("UserId"));
        tbUserRoleMiddle.setRoleId(rs.getInt("RoleId"));
        tbUserRoleMiddle.setCreateTime(rs.getString("CreateTime"));
        return tbUserRoleMiddle;
    }
}
